//package com.eshopkeeper.provider.user;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
//import org.apache.ibatis.session.RowBounds;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.CachePut;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.baomidou.mybatisplus.plugins.Page;
//import com.eshopkeeper.core.base.BaseProviderImpl;
//import com.eshopkeeper.core.util.InstanceUtil;
//import com.eshopkeeper.core.util.PropertiesUtil;
//import com.eshopkeeper.core.util.RedisUtil;
//import com.eshopkeeper.dao.user.UserSessionMapper;
//import com.eshopkeeper.model.user.UserSession;
//import com.weibo.api.motan.config.springsupport.annotation.MotanService;
//
///**
// * @author chenshufeng
// * @version 2017年3月17日20日 下午3:19:19
// */
//@CacheConfig(cacheNames = "userSession")
//@MotanService(basicService = "serviceBasicConfig")
//public class UserSessionProviderImpl extends BaseProviderImpl<UserSession> implements IUserSessionProvider {
//    @Autowired
//    private UserSessionMapper sessionMapper;
//
//    @CachePut
//    @Transactional
//    public UserSession update(UserSession record) {
//		if (record.getId() == null) {
//			record.setUpdateTime(new Date());
//			Long id = sessionMapper.queryBySessionId(record.getSessionId());
//			record.setId(id);
//			if (id != null) {
//				mapper.updateById(record);
//			} else {
//				record.setCreateTime(new Date());
//				mapper.insert(record);
//			}
//		} else {
//			mapper.updateById(record);
//		}
//        return record;
//    }
//
//    @CacheEvict
//    public void delete(final Long id) {
//        mapper.deleteById(id);
//    }
//
//    // 系统触发,由系统自动管理缓存
//    public void deleteBySessionId(final String sessionId) {
//        sessionMapper.deleteBySessionId(sessionId);
//    }
//
//    public Page<UserSession> query(Map<String, Object> params) {
//        Page<Long> page = this.getPage(params);
//        page.setRecords(mapper.selectIdByMap(page, params));
//        return getPage(page);
//    }
//
//    public List<String> querySessionIdByAccount(String account) {
//        return sessionMapper.querySessionIdByAccount(account);
//    }
//
//    //
//    public void cleanExpiredSessions() {
//        String key = "spring:session:" + PropertiesUtil.getString("session.redis.namespace") + ":sessions:expires:";
//        Map<String, Object> columnMap = InstanceUtil.newHashMap();
//        List<Long> ids = mapper.selectIdByMap(new RowBounds(), columnMap);
//        List<UserSession> sessions = getList(ids);
//        for (UserSession session : sessions) {
//            logger.info("检查SESSION : {}", session.getSessionId());
//            if (!RedisUtil.exists(key + session.getSessionId())) {
//                logger.info("移除SESSION : {}", session.getSessionId());
//                mapper.deleteById(session.getId());
//            }
//        }
//    }
//}